package com.mjlf.hometeach.common.db.provider.assit;

import com.mjlf.hometeach.common.entity.assit.Classify;
import org.apache.ibatis.jdbc.SQL;

/**
 * @auther a123
 * @create 2018-05-05 17:30
 * @desc 科目分类动态sql
 */
public class ClassifyProvider {

    /**
     * 添加科目分类
     *
     * @param classify
     *
     * @return
     */
    public String save(Classify classify) {
        SQL sql = new SQL();

        if (classify != null) {
            sql.INSERT_INTO("classify_tbl");
            if (classify.getName() != null) {
                sql.VALUES("name", "#{name}");
            }
            if (classify.getCreateTime() != null) {
                sql.VALUES("create_time", "#{createTime}");
            }
            if (classify.getModifyTime() != null) {
                sql.VALUES("modify_time", "#{modifyTime}");
            }
        }
        return sql.toString();
    }

    /**
     * 根据id删除科目分类
     *
     * @return
     */
    public String deleteById() {
        SQL sql = new SQL();
        sql.DELETE_FROM("classify_tbl").WHERE("id = #{id}");
        return sql.toString();
    }

    /**
     * 更新科目分类信息
     * @param classify
     * @return
     */
    public String updateById(Classify classify){
        SQL sql = new SQL();

        if(classify != null){
            sql.UPDATE("classify_tbl");
            if(classify.getName() != null){
                sql.SET("name = #{name}");
            }
            sql.SET("modify_time = " + System.currentTimeMillis());
        }
        return sql.toString();
    }

    /**
     * 查询指定id对应科目分类信息
     * @return
     */
    public String getClassifyById(){
        SQL sql = new SQL();
        setSELECT(sql);
        sql.FROM("classify_tbl");
        sql.WHERE("id = #{id}");
        return sql.toString();
    }

    public String listClassifyByCondition(Classify classify){
        SQL sql = new SQL();

        setSELECT(sql);
        sql.FROM("classify_tbl");
        if(classify != null){
            if(classify.getName() != null){
                sql.WHERE("name = #{name}");
            }
            if(classify.getId() != null){
                sql.WHERE("id = #{id}");
            }
        }
        return sql.toString();
    }

    private void setSELECT(SQL sql){

        sql.SELECT("id")
                .SELECT("name")
                .SELECT("create_time AS createTime")
                .SELECT("modify_time AS modifyTime")
                .SELECT("id AS classifyId");
    }
}
